package com.example.securitydemos.controller;

import com.example.securitydemos.bean.User;
import com.example.securitydemos.service.FilterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linjy on 2023/2/8
 */
@Slf4j
@RestController
public class FilterController {

    /**
     * 只返回结果中id为偶数的user元素。
     * filterObject是@PreFilter和@PostFilter中的一个内置表达式，表示集合中的当前对象。
     */
    @PostFilter("filterObject.id%2==0")
    @GetMapping("/filter/users")
    public List<User> getAllUser() {
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            users.add(new User(i, "yyg-" + i));
        }
        return users;
    }

    @Autowired
    private FilterService filterService;

    @GetMapping("/filter/users2")
    public List<Integer> getUserInfos() {
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ids.add(i);
        }

        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            users.add(new User(i, "yyg-" + i));
        }

        return filterService.doFilter(ids, users);
    }


}
